package com.itheima.controller;

import com.itheima.pojo.People;
import com.itheima.pojo.Result;
import com.itheima.service.LoginService;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RequestMapping("/account")
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    public Result login(@RequestBody People people){
        People thisPeople = loginService.login(people);
        if (thisPeople == null){
            return Result.error("账号或密码错误");
        }
        String jwt = JwtUtils.generateJwt(thisPeople.getId(),thisPeople.getName(),thisPeople.getRole(),thisPeople.getField());
        return Result.success(jwt);
    }
    @RequestMapping("/register")
    public Result register(@RequestBody People people){
        loginService.register(people);
        return Result.success();
    }
    @RequestMapping("/loginByPhone")
    public Result loginByPhone(@RequestBody People people){
        String phone = people.getPhone();
        String code = people.getPassword();
        return loginService.loginByPhone(phone,code);
    }
    @RequestMapping("/getCode")
    public Result getCode(@RequestBody People people){
        String phone = people.getPhone();
        return Result.success(loginService.getCode(phone));
    }
}
